﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using PagedList.Mvc;
using IankerProj.Models;
using System.IO;

namespace IankerProj.Controllers
{
    public class AdminController : Controller
    {
        AnkerDBEntities db = new AnkerDBEntities();
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            if (Session["user"] == null) { return RedirectToAction("Login", "Admin"); }
            return View();
        }
        public ActionResult Login()
        {
            return View();
        }
        public ActionResult Dangnhap()
        {

            string user = Request.Form["user"].ToString();
            string pass = Request.Form["password"].ToString();
            var p = db.Admin_account.Where(c => c.ussername == user).FirstOrDefault();
            if (p!= null)
            {
                if (p.password == pass)
                {
                    Session["user"] = user;
                    return RedirectToAction("Index","Admin");
                }
                

            }
            return RedirectToAction("Login", "Admin");
        }
        public ActionResult Sanpham_thietbi(int? page)
        {
            if (Session["user"] == null) { return RedirectToAction("Login", "Admin"); }
            int pageSize = 5;
            int pageNumber = (page ?? 1);
            var p = db.Sanpham_thietbi.ToList();
            return View(p.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult Sanpham(int? page)
        {
            if (Session["user"] == null) { return RedirectToAction("Login", "Admin"); }
            int pageSize = 5;
            int pageNumber = (page ?? 1);
            var p = db.Sanphams.ToList();
            return View(p.ToPagedList(pageNumber,pageSize));
        }
        public ActionResult Themsp_tb()
        {
            if (Session["user"] == null) { return RedirectToAction("Login", "Admin"); }
            ViewBag.idsp = db.Sanphams.Where(d => d.idcha != 0).ToList();
            ViewBag.idtb = db.Thietbis.ToList();
            return View();
        }
        public ActionResult Themsanpham()
        {
            if (Session["user"] == null) { return RedirectToAction("Login", "Admin"); }
            ViewBag.idcha = db.Sanphams.ToList();
            return View();
        }
        public ActionResult Themsanpham_thietbi()
        {

            sb.idsanpham = Convert.ToInt32(Request.Form["idsp"]);
            sb.idthietbi = Convert.ToInt32(Request.Form["idtb"]);
            db.Sanpham_thietbi.Add(sb);
            db.SaveChanges();

            return RedirectToAction("Sanpham_thietbi", "Admin"); 
        }
        Sanpham s = new Sanpham();
        public ActionResult Them()
        {
            s.ten=Request.Form["ten"].ToString();
            s.idcha=Convert.ToInt32(Request.Form["idcha"]);
            db.Sanphams.Add(s);
            db.SaveChanges();

            return RedirectToAction("Sanpham","Admin"); 
        }
        public ActionResult Suasptb(int term)
        {
            if (Session["user"] == null) { return RedirectToAction("Login", "Admin"); }
            var c = db.Sanpham_thietbi.Where(p => p.id == term).FirstOrDefault();
            ViewBag.idsp = db.Sanphams.Where(d=>d.idcha!=0).ToList();
            ViewBag.idtb = db.Thietbis.ToList();
            return View(c);
        }
        public ActionResult Sua(int term)
        {
            var c=db.Sanphams.Where(p=>p.id==term).FirstOrDefault();
            ViewBag.ten = c.ten;
            ViewBag.ma = c.id;
            ViewBag.idcha = db.Sanphams.ToList();
            return View();
        }
        Sanpham_thietbi sb = new Sanpham_thietbi();
        public ActionResult Suasanpham_thietbi()
        {
            var ma = Convert.ToInt32(Request.Form["ma"]);
            var idsp = Convert.ToInt32(Request.Form["idsp"]);
            var idtb = Convert.ToInt32(Request.Form["idtb"]);

            sb = db.Sanpham_thietbi.Where(c => c.id == ma).FirstOrDefault();
            sb.idsanpham = idsp;
            sb.idthietbi =idtb;

            db.SaveChanges();
            return RedirectToAction("Sanpham_thietbi", "Admin");
        }
        Sanpham q = new Sanpham();
        public ActionResult Suasanpham()
        {
            var ma = Convert.ToInt32(Request.Form["ma"]);
            var ten= Request.Form["ten"].ToString();
            var idcha = Convert.ToInt32(Request.Form["idcha"]);
            
            q = db.Sanphams.Where(c => c.id == ma).FirstOrDefault();
            q.ten = ten;
            q.idcha = idcha;

            db.SaveChanges();
           return RedirectToAction("Sanpham","Admin");
        }
        public ActionResult Taikhoan()
        {
            if (Session["user"] == null) { return RedirectToAction("Login", "Admin"); }
            string user = Session["user"].ToString();
            var c = db.Admin_account.Where(p => p.ussername == user).FirstOrDefault();
            return View(c);
        }
        public ActionResult Danhmuc(int? page)
        {
            if (Session["user"] == null) { return RedirectToAction("Login", "Admin"); }
            int pageSize = 5;
            int pageNumber = (page ?? 1);
            var p = db.Danhmucs.ToList();
            return View(p.ToPagedList(pageNumber, pageSize));
            
        }
        public ActionResult Suataikhoan()
        {
            string User = Request.Form["user"].ToString();
            string pass = Request.Form["pass"].ToString();
            ac = db.Admin_account.Where(c => c.ussername == User).FirstOrDefault();
            ac.password = pass;
            db.SaveChanges();
            return RedirectToAction("Index","Admin");
        }
        public ActionResult Logout()
        {
            Session["user"] = null;
           return RedirectToAction("Login","Admin");
        }
        public ActionResult Suadm(int term)
        {
            if (Session["user"] == null) { return RedirectToAction("Login", "Admin"); }
            var c = db.Danhmucs.Where(p => p.id == term).FirstOrDefault();
            
            ViewBag.idcha = db.Danhmucs.ToList();
            return View(c);
        }
        Danhmuc q1 = new Danhmuc();
        public ActionResult Suadanhmuc()
        {

            var ma = Convert.ToInt32(Request.Form["ma"]);
            var ten = Request.Form["ten"].ToString();
            var idcha = Convert.ToInt32(Request.Form["idcha"]);

            q1 = db.Danhmucs.Where(c => c.id == ma).FirstOrDefault();
            q1.ten = ten;
            q1.idcha = idcha;

            db.SaveChanges();
            return RedirectToAction("Danhmuc", "Admin");
        }
        public ActionResult Themdanhmuc()
        {
            if (Session["user"] == null) { return RedirectToAction("Login", "Admin"); }
            ViewBag.idcha = db.Danhmucs.ToList();
            return View();
        }
        public ActionResult Themdanhmucthietbi()
        {

            q1.ten = Request.Form["ten"].ToString();
            q1.idcha = Convert.ToInt32(Request.Form["idcha"]);
            db.Danhmucs.Add(q1);
            db.SaveChanges();

            return RedirectToAction("Danhmuc", "Admin"); 
        }
        public ActionResult Thietbi(int? page)
        {
            if (Session["user"] == null) { return RedirectToAction("Login", "Admin"); }
            int pageSize = 5;
            int pageNumber = (page ?? 1);
            var p = db.Thietbis.ToList();
            return View(p.ToPagedList(pageNumber, pageSize));
        }
        [ValidateInput(false)]
        public ActionResult Suathietbi(int term)
        {
            var c = db.Thietbis.Where(p => p.id == term).FirstOrDefault();

            ViewBag.idcha = db.Danhmucs.Where(p => p.idcha != 0).ToList();
            return View(c);
        }
        
        Thietbi tb = new Thietbi();
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult suatb(HttpPostedFileBase file,string mota,string thongso,string huongdan)
        {


            if (file != null)
            {

                string hinh = Path.GetFileName(file.FileName);
                string physicalPath = Server.MapPath("~/img/" + hinh);
                if (!System.IO.File.Exists(physicalPath))
                {
                    file.SaveAs(physicalPath);
                }
                var id = Convert.ToInt32(Request.Form["ma"]);
                var idcha = Convert.ToInt32(Request.Form["idcha"]);
                var gia = Convert.ToInt32(Request.Form["gia"]);
                var ten = Request.Form["ten"].ToString();
               
               
                tb = db.Thietbis.Where(c => c.id == id).FirstOrDefault();
                tb.ten = ten;
                tb.idcha = idcha;
                tb.hinh = hinh;
                tb.huongdan = huongdan;
                tb.mota = mota;
                tb.gia=gia;
                tb.thongso = thongso;
                db.SaveChanges();
                return RedirectToAction("Thietbi", "Admin");
            }
            else
            {
                var id = Convert.ToInt32(Request.Form["ma"]);
                var idcha = Convert.ToInt32(Request.Form["idcha"]);
                var gia = Convert.ToInt32(Request.Form["gia"]);
                var ten = Request.Form["ten"].ToString();
               
                
                tb = db.Thietbis.Where(c => c.id == id).FirstOrDefault();
                tb.ten = ten;
                tb.idcha = idcha;
                tb.gia = gia;
                tb.huongdan = huongdan;
                tb.mota = mota;
                tb.thongso = thongso;
                db.SaveChanges();
                return RedirectToAction("Thietbi", "Admin");
            }
             
        }
        public ActionResult Themtaikhoan()
        {
            if (Session["user"] == null) { return RedirectToAction("Login", "Admin"); }
            return View();
        }
        public ActionResult Themthietbi()
        {
            if (Session["user"] == null) { return RedirectToAction("Login", "Admin"); }
            ViewBag.idcha = db.Danhmucs.Where(c => c.idcha != 0).ToList() ;
            return View();
        }
        Admin_account ac = new Admin_account();
        public ActionResult Createaccount()
        {
            string user = Request.Form["user"].ToString();
            string pass = Request.Form["pass"].ToString();
            var p = db.Admin_account.Where(c => c.ussername == user).FirstOrDefault();
            if(p!=null)
            {
                RedirectToAction("Index", "Admin");
            }
            else
            {
                ac.ussername = user;
                ac.password = pass;
                db.Admin_account.Add(ac);
                db.SaveChanges();
            }
            return RedirectToAction("Login","Admin");

        }
        [ValidateInput(false)]
        public ActionResult themtb(HttpPostedFileBase file, string mota, string thongso, string huongdan)
        {
            if (file != null)
            {

                string hinh = Path.GetFileName(file.FileName);
                string physicalPath = Server.MapPath("~/img/" + hinh);
                if (!System.IO.File.Exists(physicalPath))
                {
                    file.SaveAs(physicalPath);
                }
               
                var idcha = Convert.ToInt32(Request.Form["idcha"]);
                var gia = Convert.ToInt32(Request.Form["gia"]);
                var ten = Request.Form["ten"].ToString();
          
                
                tb.ten = ten;
                tb.idcha = idcha;
                tb.hinh = hinh;
                tb.huongdan = huongdan;
                tb.mota = mota;
                tb.thongso = thongso;
                db.Thietbis.Add(tb);
                db.SaveChanges();
                return RedirectToAction("Thietbi", "Admin");
            }
            else
            {
                var id = Convert.ToInt32(Request.Form["ma"]);
                var idcha = Convert.ToInt32(Request.Form["idcha"]);
                var gia = Convert.ToInt32(Request.Form["gia"]);
                var ten = Request.Form["ten"].ToString();

                
                tb.ten = ten;
                tb.idcha = idcha;

                tb.huongdan = huongdan;
                tb.mota = mota;
                tb.thongso = thongso;
                db.Thietbis.Add(tb);
                db.SaveChanges();
                return RedirectToAction("Thietbi", "Admin");
            }
        }
        public ActionResult Xoasptb(int id)
        {
            var p = db.Sanpham_thietbi.Find(id);
            db.Sanpham_thietbi.Remove(p);
            db.SaveChanges();
            return RedirectToAction("Sanpham_thietbi", "Admin");
        }
        public ActionResult Xoasanpham(int id)
        {
            var p = db.Sanphams.Find(id);
            db.Sanphams.Remove(p);
            db.SaveChanges();
            return RedirectToAction("Sanpham", "Admin");
        }
     
        public ActionResult Xoa(int id)
        {
            var p = db.Thietbis.Find(id);
            db.Thietbis.Remove(p);
            db.SaveChanges();
            return RedirectToAction("Thietbi","Admin");
        }
    }
}
